package com.ruoyi.lock.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.lock.domain.AInbound;
import com.ruoyi.lock.domain.ASmartlocks;
import com.ruoyi.lock.service.IAInboundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 入库单Controller
 * 
 * @author ruoyi
 * @date 2024-03-15
 */
@RestController
@RequestMapping("/lock/inbound")
public class AInboundController extends BaseController
{
    @Autowired
    private IAInboundService aInboundService;

    /**
     * 查询入库单列表
     */
    @PreAuthorize("@ss.hasPermi('lock:inbound:list')")
    @GetMapping("/list")
    public TableDataInfo list(AInbound aInbound)
    {
        startPage();
        List<AInbound> list = aInboundService.selectAInboundList(aInbound);
        return getDataTable(list);
    }

    /**
     * 导出入库单列表
     */
    @PreAuthorize("@ss.hasPermi('lock:inbound:export')")
    @Log(title = "入库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AInbound aInbound)
    {
        List<AInbound> list = aInboundService.selectAInboundList(aInbound);
        ExcelUtil<AInbound> util = new ExcelUtil<AInbound>(AInbound.class);
        util.exportExcel(response, list, "入库单数据");
    }

    /**
     * 获取入库单详细信息
     */
    @PreAuthorize("@ss.hasPermi('lock:inbound:query')")
    @GetMapping(value = "/{iId}")
    public AjaxResult getInfo(@PathVariable("iId") Long iId)
    {
        return success(aInboundService.selectAInboundByIId(iId));
    }

    /**
     * 新增入库单
     */
    @PreAuthorize("@ss.hasPermi('lock:inbound:add')")
    @Log(title = "入库单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AInbound aInbound)
    {
        aInbound.setResidueNum(aInbound.getQuantity());
        return toAjax(aInboundService.insertAInbound(aInbound));
    }

    /**
     * 修改入库单
     */
    @PreAuthorize("@ss.hasPermi('lock:inbound:edit')")
    @Log(title = "入库单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AInbound aInbound)
    {
        return toAjax(aInboundService.updateAInbound(aInbound));
    }

    /**
     * 删除入库单
     */
    @PreAuthorize("@ss.hasPermi('lock:inbound:remove')")
    @Log(title = "入库单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{iIds}")
    public AjaxResult remove(@PathVariable Long[] iIds)
    {
        return toAjax(aInboundService.deleteAInboundByIIds(iIds));
    }


    /**
     * 产品出库
     */
//    @PreAuthorize("@ss.hasPermi('lock:inbound:add')")
    @Log(title = "产品出库", businessType = BusinessType.INSERT)
    @PostMapping("/productsOutbound")
    public AjaxResult productsOutbound(@RequestBody ASmartlocks aSmartlocks)
    {
        return toAjax(aInboundService.productsOutbound(aSmartlocks));
    }

    /**
     * 设备闲置数（剩余数）
     */
    @GetMapping("/num")
    public AjaxResult getIdleQuantity()
    {
        return success(aInboundService.getIdleQuantity());
    }


    /**
     * 收回
     */
    @GetMapping("/recallLock")
    public AjaxResult recallLock(@RequestParam("mac") String mac){
        int result = aInboundService.recallLock(mac);
        if(result == 0){
            return AjaxResult.error("不能收回已安装的设备");
        }else if(result == 1){
            return AjaxResult.success("收回成功");
        }else if(result == 4){
            return AjaxResult.error("门锁设备没有这把锁,收回失败");
        }else{
            return AjaxResult.error("收回失败");
        }
    }
}
